﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace NanoERP.Common
{
    /// <summary>
    /// 界面控件操作类
    /// </summary>
    public class ControlUtil
    {
        /// <summary>
        /// 清除容器里面某些控件的值
        /// </summary>
        /// <param name="parContainer">容器类控件</param>
        public static void ClearControlValue(Control parContainer)
        {
            foreach (Control c in parContainer.Controls)
            {
                // 如果是容器类控件，递归调用自己
                if (c.HasChildren)
                    ClearControlValue(c);
                else
                {
                    switch (c.GetType().Name)
                    {
                        case "RichTextBox": ((RichTextBox)c).Text = string.Empty; break;
                        case "TextBox": ((TextBox)c).Text = string.Empty; break;
                        case "CheckBox": ((CheckBox)c).Checked = false; break;
                        case "RadioButton": ((RadioButton)c).Checked = false; break;
                        case "ComboBox": ((ComboBox)c).Text = string.Empty; break;
                    }
                }
            }
        }

        /// <summary>
        /// 清除容器里面绑定的控件
        /// </summary>
        /// <param name="parContainer">容器类控件</param>
        public static void ClearControlBind(Control parContainer)
        {
            foreach (Control c in parContainer.Controls)
            {
                if (c.GetType().Name.Equals("LabelX")) 
                    continue;
                // 如果是容器类控件，递归调用自己
                if (c.HasChildren)
                    ClearControlBind(c);
                else
                {
                    c.DataBindings.Clear();
                }
            }
        }

        /// <summary>
        /// DataGridView[X] 行绘制
        /// </summary>
        /// <param name="sender">需要绘制的控件</param>
        /// <param name="e">DataGridViewRowPostPaintEventArgs</param>
        public static void DataGridRowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            var dgv = sender as DevComponents.DotNetBar.Controls.DataGridViewX;
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
                  e.RowBounds.Location.Y,
                  dgv.RowHeadersWidth - 4,
                  e.RowBounds.Height);

            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
                dgv.RowHeadersDefaultCellStyle.Font,
                rectangle,
                dgv.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

    }
}
